#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@Time       : 2019/11/28 17:38
@Author     : LeeCQ
@File Name  : Mzitu_info_only.py
"""
import re, time, json
import requests

from HTTP.Download_Mzitu.Mzitu import MzituDownload
from HTTP.Download_Mzitu.Mzitu_db import MzituDBNoJSON


class MzituInfo(MzituDownload):
    """
    这个类仅仅更新Mzitu的data字段；
    不下载图片；
    """
    def __init__(self):
        super().__init__()
        self._db = MzituDBNoJSON()

    def one_album(self, album_url):
        """
        取得一个相册中没证图片的绝对URL，和该相册的相关信息
        :param album_url: 这个相册的URL
        :return: data = {'idd': idd,
                         'title': re.sub('[:*?"<>|]', '-', title),
                         'times': times,
                         'tags': tags,
                         'images': images
        }
        """
        # 判断是否已经下载
        try:
            if self._db.view_column('mzitu', 'DOWNLOAD', WHERE=f'ALBUM_URL="{album_url}"')[0][0] == 'Y':
                return 1, album_url
        except IndexError: pass
        # album_url = 'https://www.mzitu.com/157154'
        soup = self.open_url(album_url, self.HEADERS)
        print(self.one_album.__name__, f'正在构建相册{album_url}中的URL...', end='\t')
        _start_one = time.time()
        idd = re.search(r'\d+', album_url).group()
        # print(soup.select('h2.main-title'))
        title = soup.select('h2.main-title')[0].text  # 获得套图的标题[0]
        tags = soup.select('div.main-meta > span')[0].text.replace('分类：', '').strip()  # 获得分类
        times = soup.select('div.main-meta > span')[1].text.replace('发布于', '').strip()  # 获得发布时间
        page_nums = soup.select('div.pagenavi > a')[-2].text  # 获得页码
        images_model = self.open_url(album_url, self.HEADERS).select('div.main-image > p > a > img')[0].get('src')
        pic_url = [album_url + '/' + str(num) for num in range(1, int(page_nums) + 1)]  # 获取图片每个页面的URL，装入列表
        images = [images_model[:-6] + str(num).rjust(2, '0') + '.jpg' for num in range(1, int(page_nums)+1)]
        # 获取每张图片的URL，装入列表
        print(self.one_album.__name__, "建构时间：", time.time() - _start_one)
        data = {
            'idd': idd,
            'title': re.sub('[:*?"<> |]', '-', title),
            'times': times,
            'tags': tags,
            'pagePhoto': pic_url,
            'images': images,
        }
        print(f"正在更新数据库{self._db.sheet_all_album}的{idd}项...", end='\t')
        if not self._db.all_album_update(idd, 'TIME', times, TAGS=tags, DATA=json.dumps(data)):
            print("更新成功")
        else:
            print('更行失败')
        # exit()
        return data

    def MAX_data(self):
        list_data = self._db.view_column('Mzitu', 'DATA')
        MAX = 0
        for data in list_data:
            if MAX < len(data[0]): MAX = len(data[0])
        print(MAX)

    def run(self):
        print("爬取开始...")
        n = 0
        while True:
            try:
                for album in self.all_albums(self.URL, self.HEADERS):
                    idd = album.split('/')[-1]
                    tags = self._db.view_column('Mzitu_no_json', "TAGS", WHERE=f'IDD={idd}')[0][0]
                    print('\r', tags)
                    if tags is None:
                        print(f'** {idd}'.ljust(50, '-'))
                        self.one_album(album)
                        time.sleep(0)
                    n += 1
                break
            except IndexError and requests.exceptions.ConnectTimeout: pass


if __name__ == '__main__':
    program_start = time.time()
    MzituInfo().run()
    print("程序运行时间：", time.time()-program_start)
